<template>
  <div class="dialog-form">
    <div class="content">
      <div class="print-box" id="printBox">
        <div class="item" v-for="(item, index) in list" :key="index">
          <div class="code-box">
            <canvas class="code" :id="'code'+index" width="70" height="70"></canvas>
          </div>
          <p class="text">{{ item.assetName }}</p>
          <p class="depart">编码：{{ item.assetNumber }}</p>
          <p class="depart">部门：{{ item.departName }}</p>
        </div>
      </div>
    </div>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" v-print="printObj">打印</el-button>
    </div>
  </div>
</template>

<script setup>
import UQRCode from 'uqrcodejs'

const emits = defineEmits(['cancel', 'confirm'])
const props = defineProps(['list'])

const pageInit = () => {

}

const printObj = ref({
  id: 'printBox',
  popTitle: '资产二维码',
  previewTitle: '资产二维码',
  previewPrintBtnLabel: '预览结束，开始打印',
  openCallback(res) {
    console.log('显示打印面板')
  },
  closeCallback(res) {
    console.log('关闭打印面板')
  },
  clickMounted() {
    console.log('开始打印')
  }
})

onMounted(() => {
  props.list.forEach((obj, index) => {
    const codeId = 'code'+index
    var qr = new UQRCode()
    qr.data = obj.assetNumber
    qr.size = 70
    qr.make()
    var canvas = document.getElementById(codeId)
    var canvasContext = canvas.getContext("2d")
    qr.canvasContext = canvasContext
    qr.drawCanvas()
  })
})

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>
.content {
  overflow-y: scroll;
  display: flex;
  justify-content: center;
}

.print-box {
  width: 300px;

  .item {
    position: relative;
    width: 100%;
    height: 100px;
    page-break-after: always;
    font-size: 0;
    box-sizing: border-box;

    .code-box {
      position: absolute;
      margin-top: 15px;
      right: 10px;
    }

    .code {
      width: 70px;
      height: 70px;
    }

    .text, .depart {
      position: relative;
      padding-left: 10px;
      padding-top: 12px;
      box-sizing: border-box;
      color: #333;
      font-size: 12px;
      width: 210px;
    }

    .depart {
      padding-top: 5px;
    }
  }
}

@media screen and (min-width: 1280px) {
  .content {
    max-height: 600px;
  }
}

@media screen and (max-width: 1280px) {
  .content {
    height: 400px;
  }
}

@media print {

  .print-box {
    height: 24mm;

    .item {
      position: relative;
      width: 100%;
      height: 24mm;
      page-break-after: always;
      font-size: 0;
      box-sizing: border-box;

      .code-box {
        position: absolute;
        margin-top: 4mm;
        right: 3mm;
      }

      .code {
        width: 18mm;
        height: 18mm;
      }

      .text, .depart {
        position: relative;
        padding-left: 2mm;
        padding-top: 2.5mm;
        box-sizing: border-box;
        color: #333;
        font-size: 12px;
        width: calc(100% - 24mm);
      }

      .depart {
        padding-top: 1mm;
      }
    }
  }
}
</style>